﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Enter.Common.statics
{
	public sealed class RandomCode
	{
		private static readonly int _FillSeats_Size=10;

		private static readonly string _FillSeats_FillCode = "*";

		/// <summary>
		/// 生成随机数
		/// </summary>
		/// <param name="num"></param>
		/// <returns></returns>
		public static string GenerateCode(int num)
		{
			int number;
			char code;
			StringBuilder checkCode = new StringBuilder(num);
			System.Random random = new Random();

			for (int i = 0; i < num; i++)
			{
				number = random.Next();

				if (number % 2 == 0)
					code = (char)('1' + (char)(number % 9));
				else
					code = (char)('A' + (char)(number % 26));

				checkCode.Append(code);
			}
			return checkCode.ToString();

		}

		/// <summary>
		/// 自动补位
		/// </summary>
		/// <param name="Code">原代码</param>
		/// <returns></returns>
		public static string FillSeats(string Code)
		{
			return FillSeats(Code, _FillSeats_Size, _FillSeats_FillCode);
		}

		/// <summary>
		///  自动补位
		/// </summary>
		/// <param name="Code">原代码</param>
		/// <param name="Size">字符长度</param>
		/// <returns></returns>
		public static string FillSeats(string Code, int Size)
		{
			return FillSeats(Code, Size, _FillSeats_FillCode);
		}

		/// <summary>
		///  自动补位
		/// </summary>
		/// <param name="Code">原代码</param>
		/// <param name="size">字符长度</param>
		/// <param name="FillCode">补位字符</param>
		/// <returns></returns>
		public static string FillSeats(string Code, int size, string FillCode)
		{
			int s = Code.Length;
			StringBuilder sb = new StringBuilder(size);
			for (int i = s; i < size; i++)
			{
				sb.Append(FillCode);
			}
			sb.Append(Code);
			return sb.ToString();
		}

		/// <summary>
		/// 移除补位
		/// </summary>
		/// <param name="Code"></param>
		/// <returns></returns>
		public static string RemoveSeats(string Code)
		{
			return RemoveSeats(Code, _FillSeats_FillCode);
		}

		/// <summary>
		/// 移除补位
		/// </summary>
		/// <param name="Code"></param>
		/// <param name="FillCode"></param>
		/// <returns></returns>
		public static string RemoveSeats(string Code, string FillCode)
		{
			char[] xx = Code.ToCharArray();
			int yuan=0;
			int count = xx.Length;
			for (int i = 0; i < count; i++)
			{
			   if (xx[i].ToString()!=FillCode)
			   {
				   yuan = i;
				   break;
			   }
			}
			StringBuilder sb = new StringBuilder(count-yuan);
			for (int i = yuan; i < count; i++)
			{
				sb.Append(xx[i]);
			}
			//sb.Append(Code);

			return sb.ToString();
		}


	}
}
